package cn.i96688.scanclient.ui;

import java.awt.BorderLayout;
import java.awt.Rectangle;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JProgressBar;

@SuppressWarnings("serial")
public class PaymentProgress extends JDialog {
	private int width = 200;
	private int height = 60;
	private volatile boolean isRun=false;
	private JProgressBar progressBar;
	private ProgressThread pThread;
	
	
	private OrderQueryListener orderQuery=null;

	public PaymentProgress(JFrame frame) {
		super(frame, true);
//		super.setUndecorated(true);
		this.setIconImage(MainForm.icon);
		this.setTitle("   支付中...");
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		setLayout(new BorderLayout());
		progressBar = new JProgressBar(0, 100);
		add(progressBar, BorderLayout.CENTER);
		setSize(width, height);
		
		pThread=new ProgressThread(progressBar);
		pThread.start();
	}
	
	

	public void showForm() {
		Rectangle rect = getParent().getBounds();
		int x = rect.x + (rect.width - width) / 2;
		int y = rect.y + (rect.height - height) / 2;
		this.setBounds(x, y, width, height);
		setAlwaysOnTop(true);
		isRun=true;
		progressBar.setValue(progressBar.getMaximum());
//		PoolUtil.execute(new BarThread(progressBar));
		pThread.unLock();
		setVisible(true);
	}
	
	

	public void hideForm() {
		setVisible(false);
	}
	@Override
	public void setVisible(boolean b){
		if(!b){
			isRun=false;
			pThread.lock(false);
		}
		super.setVisible(b);
	}
	
	class ProgressThread extends Thread{
		private Object locked=new Object();
		private boolean isLock=true;
		private JProgressBar progress;
		int DELAY = 30;
		public ProgressThread(JProgressBar progress) {
			this.progress = progress;
		}
		@Override
		public void run(){
			while(true){
				try {
					startLock();
					runGress();
				} catch (Exception e) {
				}
			}
		}
		void runGress(){
			int minimum = progress.getMinimum();
			progress.setValue(minimum);
			int maximum = progress.getMaximum();
			for (int i = minimum; i < maximum; i++) {
				try {
					int value = progress.getValue();
					progress.setValue(value + 1);
					Thread.sleep(DELAY);
				} catch (InterruptedException ignoredException) {
				}
			}
			lock(true);
		}
		void lock(boolean query){
			if(!isLock){
				isLock=true;
				progress.setValue(0);
			}
			if(orderQuery!=null && query){
				orderQuery.query();
			}
		}
		void startLock(){
			if(isLock){
				try {
					synchronized(locked){
						locked.wait();
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		void unLock(){
			try {
				synchronized(locked){
					locked.notify();
				}
				isLock=false;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	class BarThread implements Runnable {
		private JProgressBar progress;
		int DELAY = 100;
		BarThread(JProgressBar progress) {
			this.progress = progress;
		}
		
		@Override
		public void run() {
			int minimum = progress.getMinimum();
			progress.setValue(minimum);
			int maximum = progress.getMaximum();
			for (int i = minimum; i < maximum && isRun; i++) {
				try {
					int value = progress.getValue();
//					System.out.println(isRun+"   "+value);
					progress.setValue(value + 1);
					Thread.sleep(DELAY);
				} catch (InterruptedException ignoredException) {
				}
			}
			if(orderQuery!=null && isRun){
				orderQuery.query();
			}
		}
	}
	
	public void setOrderQueryListener(OrderQueryListener l){
		orderQuery=l;
	}
	
	public interface OrderQueryListener{
		void query();
	}
}
